//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Mail;
using DotNetNuke.Services.Vendors;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Vendors
{
	partial class EditVendors : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		public int VendorID = -1;
		private void ReturnUrl(string Filter)
		{
			if (string.IsNullOrEmpty(Filter.Trim())) {
				Response.Redirect(Common.Globals.NavigateURL(), true);
			} else {
				Response.Redirect(Common.Globals.NavigateURL(this.TabId, Null.NullString, "filter=" + Filter), true);
			}
		}
		private void AddModuleMessage(string message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType type)
		{
			UI.Skins.Skin.AddModuleMessage(this, Localization.GetString(message, LocalResourceFile), type);
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);

			try {
				TabController objTabs = new TabController();
				TabInfo objTab;
				ModuleController objModules = new ModuleController();
				bool blnBanner = false;
				bool blnSignup = false;
				if ((Request.QueryString["VendorID"] != null)) {
					VendorID = Int32.Parse(Request.QueryString["VendorID"]);
				}
				if (Request.QueryString["ctl"] != null && VendorID == -1) {
					blnSignup = true;
				}
				if (Request.QueryString["banner"] != null) {
					blnBanner = true;
				}
				if (Page.IsPostBack == false) {
					ctlLogo.FileFilter = Common.Globals.glbImageFileTypes;
					addresssVendor.ModuleId = ModuleId;
					addresssVendor.StartTabIndex = 4;
					ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
					Services.Vendors.ClassificationController objClassifications = new Services.Vendors.ClassificationController();
					ArrayList Arr = objClassifications.GetVendorClassifications(VendorID);
					int i;
					for (i = 0; i <= Arr.Count - 1; i++) {
						ListItem lstItem = new ListItem();
						Services.Vendors.ClassificationInfo objClassification = (ClassificationInfo)Arr[i];
						lstItem.Text = objClassification.ClassificationName;
						lstItem.Value = objClassification.ClassificationId.ToString();
						lstItem.Selected = objClassification.IsAssociated;
						lstClassifications.Items.Add(lstItem);
					}
					Services.Vendors.VendorController objVendors = new Services.Vendors.VendorController();
					if (VendorID != -1) {
						Services.Vendors.VendorInfo objVendor;
						if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId && UserInfo.IsSuperUser) {
							objVendor = objVendors.GetVendor(VendorID, Null.NullInteger);
						} else {
							objVendor = objVendors.GetVendor(VendorID, PortalId);
						}
						if (objVendor != null) {
							txtVendorName.Text = objVendor.VendorName;
							txtFirstName.Text = objVendor.FirstName;
							txtLastName.Text = objVendor.LastName;
							ctlLogo.Url = objVendor.LogoFile;
							addresssVendor.Unit = objVendor.Unit;
							addresssVendor.Street = objVendor.Street;
							addresssVendor.City = objVendor.City;
							addresssVendor.Region = objVendor.Region;
							addresssVendor.Country = objVendor.Country;
							addresssVendor.Postal = objVendor.PostalCode;
							addresssVendor.Telephone = objVendor.Telephone;
							addresssVendor.Fax = objVendor.Fax;
							addresssVendor.Cell = objVendor.Cell;
							txtEmail.Text = objVendor.Email;
							txtWebsite.Text = objVendor.Website;
							chkAuthorized.Checked = objVendor.Authorized;
							txtKeyWords.Text = objVendor.KeyWords;
							ctlAudit.CreatedByUser = objVendor.CreatedByUser;
							ctlAudit.CreatedDate = objVendor.CreatedDate.ToString();
						}
						var objBanners = this.LoadControl("~" + this.TemplateSourceDirectory.Remove(0, Common.Globals.ApplicationPath.Length) + "/Banners.ascx");
						objBanners.ID = "/Banners.ascx";
						//objBanners.VendorID = this.VendorID;
						//objBanners.ModuleConfiguration = ModuleConfiguration;
						divBanners.Controls.Add(objBanners);
						var objAffiliates = this.LoadControl("~" + this.TemplateSourceDirectory.Remove(0, Common.Globals.ApplicationPath.Length) + "/Affiliates.ascx");
						objAffiliates.ID = "/Affiliates.ascx";
						//objAffiliates.VendorID = this.VendorID;
						//objAffiliates.ModuleConfiguration = ModuleConfiguration;
						divAffiliates.Controls.Add(objAffiliates);
					} else {
						chkAuthorized.Checked = true;
						pnlAudit.Visible = false;
						cmdDelete.Visible = false;
						pnlBanners.Visible = false;
						pnlAffiliates.Visible = false;
					}
					if (blnSignup == true || blnBanner == true)
					{
						rowVendor1.Visible = false;
						rowVendor2.Visible = false;
						pnlVendor.Visible = false;
						cmdDelete.Visible = false;
						pnlAudit.Visible = false;
						if (blnBanner == true) {
							cmdUpdate.Visible = false;
						} else {
							cmdUpdate.Text = "Signup";
						}
					} else {
						if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId) {
							objTab = objTabs.GetTabByName("Vendors", Null.NullInteger);
						} else {
							objTab = objTabs.GetTabByName("Vendors", PortalId);
						}
						if (objTab != null) {
							ViewState["filter"] = Request.QueryString["filter"];
						}
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdCancel_Click(object sender, System.EventArgs e)
		{
			try {
				ReturnUrl(Convert.ToString(ViewState["filter"]));
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdDelete_Click(object sender, System.EventArgs e)
		{
			try {
				if (VendorID != -1) {
					VendorController objVendors = new VendorController();
					objVendors.DeleteVendor(VendorID);
				}
				Response.Redirect(Common.Globals.NavigateURL());
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try {
				int intPortalID;
				string strLogoFile = "";
				if (Page.IsValid) {
					if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId) {
						intPortalID = -1;
					} else {
						intPortalID = PortalId;
					}
					VendorController objVendors = new VendorController();
					VendorInfo objVendor = new VendorInfo();
					objVendor.PortalId = intPortalID;
					objVendor.VendorId = VendorID;
					objVendor.VendorName = txtVendorName.Text;
					objVendor.Unit = addresssVendor.Unit;
					objVendor.Street = addresssVendor.Street;
					objVendor.City = addresssVendor.City;
					objVendor.Region = addresssVendor.Region;
					objVendor.Country = addresssVendor.Country;
					objVendor.PostalCode = addresssVendor.Postal;
					objVendor.Telephone = addresssVendor.Telephone;
					objVendor.Fax = addresssVendor.Fax;
					objVendor.Cell = addresssVendor.Cell;
					objVendor.Email = txtEmail.Text;
					objVendor.Website = txtWebsite.Text;
					objVendor.FirstName = txtFirstName.Text;
					objVendor.LastName = txtLastName.Text;
					objVendor.UserName = UserInfo.UserID.ToString();
					objVendor.LogoFile = ctlLogo.Url;
					objVendor.KeyWords = txtKeyWords.Text;
					objVendor.Authorized = chkAuthorized.Checked;
					if (VendorID == -1) {
						try {
							VendorID = objVendors.AddVendor(objVendor);
						} catch {
							AddModuleMessage("ErrorAddVendor", DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
							return;
						}
					} else {
						VendorInfo objVendorCheck = new VendorInfo();
						objVendorCheck = objVendors.GetVendor(VendorID, intPortalID);
						if (objVendorCheck != null) {
							objVendors.UpdateVendor(objVendor);
						} else {
							Response.Redirect(Common.Globals.NavigateURL());
						}
					}
					ClassificationController objClassifications = new ClassificationController();
					objClassifications.DeleteVendorClassifications(VendorID);
					foreach (ListItem lstItem in lstClassifications.Items) {
						if (lstItem.Selected) {
							objClassifications.AddVendorClassification(VendorID, Int32.Parse(lstItem.Value));
						}
					}
					if (cmdUpdate.Text == "Signup") {
						ArrayList Custom = new ArrayList();
						Custom.Add(DateTime.Now.ToString());
						Custom.Add(txtVendorName.Text);
						Custom.Add(txtFirstName.Text);
						Custom.Add(txtLastName.Text);
						Custom.Add(addresssVendor.Unit);
						Custom.Add(addresssVendor.Street);
						Custom.Add(addresssVendor.City);
						Custom.Add(addresssVendor.Region);
						Custom.Add(addresssVendor.Country);
						Custom.Add(addresssVendor.Postal);
						Custom.Add(addresssVendor.Telephone);
						Custom.Add(addresssVendor.Fax);
						Custom.Add(addresssVendor.Cell);
						Custom.Add(txtEmail.Text);
						Custom.Add(txtWebsite.Text);
						//send email to Admin
                        Mail.SendEmail(PortalSettings.Email, PortalSettings.Email, Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_VENDOR_REGISTRATION_ADMINISTRATOR_SUBJECT"),
                                        Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_VENDOR_REGISTRATION_ADMINISTRATOR_BODY", Services.Localization.Localization.GlobalResourceFile, Custom));


                        //send email to vendor
                        Custom.Clear();
                        Custom.Add(txtFirstName.Text);
                        Custom.Add(txtLastName.Text);
                        Custom.Add(txtVendorName.Text);

                        Mail.SendEmail(PortalSettings.Email, txtEmail.Text, Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_VENDOR_REGISTRATION_SUBJECT"),
                                        Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_VENDOR_REGISTRATION_BODY", Services.Localization.Localization.GlobalResourceFile, Custom));


                        ReturnUrl(txtVendorName.Text.Substring(0, 1));
					} else {
						ReturnUrl(Convert.ToString(ViewState["filter"]));
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
